package com.guigu.serviceorder.service.impl;

import com.github.wxpay.sdk.Config;
import com.github.wxpay.sdk.WXPayRequest;
import com.github.wxpay.sdk.WXPayUtil;
import com.guigu.serviceorder.service.ITbOrderService;
import com.guigu.serviceorder.service.WeixinPayService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class WeixinPayServiceImpl implements WeixinPayService {

    @Autowired
    private ITbOrderService orderService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public Map createNative(String orderId, Integer money, String notifyUrl) {
        Config config = new Config();
        try {
            //1.创建参数
            Map<String,String> param=new HashMap();//创建参数
            param.put("appid", config.getAppID());//公众号
            param.put("mch_id", config.getMchID());//商户号
            param.put("nonce_str", WXPayUtil.generateNonceStr());//随机字符串
            param.put("body", "青橙");//商品描述
            param.put("out_trade_no", orderId);//商户订单号
            param.put("total_fee",money+"");//总金额（分）
            param.put("spbill_create_ip", "127.0.0.1");//IP
            param.put("notify_url", notifyUrl );//暂时随便写一个
            param.put("trade_type", "NATIVE");//交易类型
            String xmlParam = null;
            xmlParam = WXPayUtil.generateSignedXml(param, config.getKey());
            //2.发送请求
            WXPayRequest wxPayRequest = new WXPayRequest(config);
            String result = wxPayRequest.requestWithCert("/pay/unifiedorder", null, xmlParam, false);
            //3.封装结果
            System.out.println(result);
            Map<String, String> resultMap = WXPayUtil.xmlToMap(result);
            Map<String, String> map=new HashMap();
            map.put("code_url", resultMap.get("code_url"));//支付地址
            map.put("total_fee", money+"");//总金额
            map.put("out_trade_no",orderId);//订单号
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            return new HashMap();
        }
    }

    @Override
    public void notifyLogic(String xml) {
        Config config = new Config();
        try {
            //1.对xml进行解析 map
            Map<String, String> map = WXPayUtil.xmlToMap(xml);
            //2.验证签名
            boolean signatureValid = WXPayUtil.isSignatureValid(map, config.getKey());
            System.out.println("验证签名是否正确："+signatureValid);
            System.out.println(map.get("out_trade_no"));
            System.out.println(map.get("result_code"));
            if (signatureValid){
                if ("SUCCESS".equals(map.get("result_code"))){
                    orderService.updatePayStatus(map.get("out_trade_no"),map.get("transaction_id"));
                    // 发送订单号给mq
                    rabbitTemplate.convertAndSend("paynotify","",map.get("out_trade_no"));
                }else {
                    // 记录日志
                }
            }else {
                // 记录日志
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
